package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.bean.User;
import com.briup.bean.UserExtend;
import com.briup.exception.ServiceException;
import com.briup.service.UserService;
import com.briup.mapper.UserMapper;
import com.briup.util.MD5Util;
import com.briup.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 梦晓
* @description 针对表【cms_user】的数据库操作Service实现
* @createDate 2024-11-18 14:40:06
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    UserMapper userMapper;

    @Override
    public void deleteByBatch(List<Long> ids) {
        if (ids==null||ids.isEmpty()){
            throw new ServiceException(ResultCode.PARAM_IS_INVALID);
        }
        userMapper.deleteBatchIds(ids);
    }

    @Override
    public User queryById(Long id) {
        if (id==null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        User user =userMapper.selectById(id);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        return user;
    }

    @Override
    public IPage<UserExtend> query(Integer pageNum, Integer pageSize, String username,
                                   String status, Integer roleId, Integer isVip) {
        if (pageNum==null||pageSize==null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        IPage<UserExtend> page = new Page<>(pageNum,pageSize);
        userMapper.selectAll(page,username,status,roleId,isVip);
        return page;
    }

    @Override
    public User login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username).eq(User::getPassword,password);
        User user = userMapper.selectOne(lqw);
        if (user==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        return user;
    }

    @Override
    public void insert(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (username==null||password==null){
            throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
        }
        user.setUsername(username);
        user.setPassword(MD5Util.MD5(password));
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User u = userMapper.selectOne(lqw);
        if (u!=null){
            throw new ServiceException(ResultCode.USER_HAS_EXISTED);
        }
        user.setRegisterTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public void setVip(Long id) {
        //1.id判断是否有效
        User user = userMapper.selectById(id);
        if (user == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        //2.判断是否已经设置为vip
        if (user.getIsVip() == 1)
            throw new ServiceException(ResultCode.PARAM_IS_INVALID);
        //3.修改用户为vip，同时修改过期时间expires_time
        User u = new User();
        u.setId(id);
        u.setIsVip(1);
        LocalDateTime expiresTime = LocalDateTime.now().plusMonths(1);
        u.setExpiresTime(expiresTime);
        userMapper.updateById(u);
    }

    @Override
    public void myUpdate(User user) {
        Long id = user.getId();
        User user1 = userMapper.selectById(id);
        if (id == null || user1 == null) {
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        String newName = user.getUsername();
        String oldName = user1.getUsername();
        if (newName != null && !newName.equals(oldName)) {
            String trimName = newName.trim();
            if ("".equals(trimName))
                throw new
                        ServiceException(ResultCode.PARAM_IS_INVALID);
            //更新username(已经去除前后空白字符)
            user.setUsername(trimName);
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getUsername, trimName);
            if (userMapper.selectOne(lqw) != null) {
                throw new ServiceException(ResultCode.USERNAME_HAS_EXISTED);
            }
        }
        userMapper.updateById(user);
    }
}




